#!/usr/bin/env python
# coding=utf-8
# __author__ = 'Yunchao Ling'

from pymongo import MongoClient


# 连接MongoDB
def InitMongoDB(MONGODB_HOST, MONGODB_PORT, MONGODB_DB_NAME, MONGODB_COLLECTION_NAME):
    client = MongoClient(MONGODB_HOST, MONGODB_PORT)
    db = client[MONGODB_DB_NAME]
    collection = db[MONGODB_COLLECTION_NAME]
    return client, collection


# 关闭MongoDB
def CloseMongoDB(MongoDB_Connection):
    MongoDB_Connection.close()


def getFields_morphology_physiology(MongoDB_Collection):
    nameset = set()
    results = MongoDB_Collection.find({"morphology_physiology": {"$exists": 1}})
    for result in results:
        item = result["morphology_physiology"]
        for key in item.keys():
            if key not in nameset:
                nameset.add(key)
    return nameset


def getFields_culture_growth_condition(MongoDB_Collection):
    nameset = set()
    results = MongoDB_Collection.find({"culture_growth_condition": {"$exists": 1}})
    for result in results:
        item = result["culture_growth_condition"]
        for key in item.keys():
            if key not in nameset:
                nameset.add(key)
    return nameset


def getFields(MongoDB_Collection, fieldName):
    nameset = set()
    results = MongoDB_Collection.find({fieldName: {"$exists": 1}})
    for result in results:
        item = result[fieldName]
        for key in item.keys():
            if key not in nameset:
                nameset.add(key)
    return nameset


def statSpeciesLMSG():
    zero_count = 0
    unique_count = 0
    double_count = 0
    total_count = 0

    bacdive_client, bacdive_collection = InitMongoDB("10.188.188.22", 27017, "Pre_Data", "bacdive")

    lmsg_client, lmsg_collection = InitMongoDB("10.188.188.22", 27017, "Pre_Data", "imsg")

    results = lmsg_collection.find({"Rank": "Species"})
    for result in results:
        total_count += 1
        if "names_scientific_name" in result:
            scientific_name = result["names_scientific_name"]
            count = bacdive_collection.count({"taxonomy_name.strains_tax_PNU.species": scientific_name,
                                              "taxonomy_name.strains_tax_PNU.is_type_strain": True})
            if count == 0:
                zero_count += 1
            elif count == 1:
                unique_count += 1
            elif count == 2:
                double_count += 1
            print(scientific_name + "=" + str(count))
        else:
            print(result)
    bacdive_client.close()
    lmsg_client.close()

    print("total_count=" + str(total_count) + ";zero_count=" + str(zero_count) + ";unique_count=" + str(unique_count))


if __name__ == '__main__':
    # client, collection = InitMongoDB()
    # RecurNode(sys.argv[1], collection)
    # morphology_physiology=getFields_morphology_physiology(collection)
    # for item in morphology_physiology:
    #     print(item)
    # culture_growth_condition = getFields_culture_growth_condition(collection)
    # for item in culture_growth_condition:
    #     print(item)
    # fields = getFields(collection, "strain_availability")
    # for item in fields:
    #     print(item)
    # CloseMongoDB(client)
    statSpeciesLMSG()
